package com.ftg.learn.controller;

import com.ftg.learn.service.Iuser;
import com.ftg.learn.service.impl.UserImpl;
import com.ftg.learn.util.BaseResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用来登陆
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/24 上午8:31
 */
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // controller功能描述

        //转码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        // 接值
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 调方法  ---> service
        Iuser iuser = new UserImpl();
        BaseResultSet brs = iuser.loginByUsernamePassword(username, password);
        //存储
        req.setAttribute("info", brs);

        //转页
        int success = 200;
        if (success == brs.getCode()) {
            req.getRequestDispatcher("/layout/home.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
    }
}
